# -*- coding: utf-8 -*-
import urllib
import urlparse
import re
import datetime
from bs4 import BeautifulSoup
from bs4 import Tag
import urllib2
import logging
import time

listUrl = 'https://b2b.10086.cn/b2b/main/listVendorNoticeResult.html?noticeBean.noticeType=2'
params = {'page.currentPage': '1', 'page.perPageSize': '500', 'noticeBean.sourceCH': '',
          'noticeBean.source': '', 'noticeBean.title': '四川', 'noticeBean.startDate': '', 'noticeBean.endDate': ''}

params2 = {'page.currentPage': '1', 'page.perPageSize': '500', 'noticeBean.sourceCH': '四川',
          'noticeBean.source': '', 'noticeBean.title': '', 'noticeBean.startDate': '', 'noticeBean.endDate': ''}

contentUrl = 'https://b2b.10086.cn/b2b/main/viewNoticeContent.html?noticeBean.id='
attachmentUrlPre = 'https://b2b.10086.cn'

todayids=[]

def getTitlematch():
    ids = []
    data = urllib.urlencode(params)
    response = urllib2.urlopen(listUrl, data)
    # response = urllib2.urlopen(req)
    html = response.read().decode('utf-8')
    soup = BeautifulSoup(html, "html.parser")

    # soup = BeautifulSoup(open('example.html', encoding='utf-8'), 'html.parser')
    #  nodes = soup.find_all('tr', class_=[" ", "jtgs_table_td_bg"])
    nodes = soup.find_all('tr', attrs={'onmousemove': 'cursorOver(this)'})
    for node in nodes:
        date = node.select('td:nth-of-type(4)')[0].text
        if isToday(date):
            onclick = node.attrs['onclick']
            biddingId = re.findall(r"\d+", onclick)
            ids = ids + biddingId
            logging.info("today's bidding id:" + biddingId[0])
    return ids

def getLocalMatch():
    ids = []
    data = urllib.urlencode(params2)
    response = urllib2.urlopen(listUrl, data)
    # response = urllib2.urlopen(req)
    html = response.read().decode('utf-8')
    soup = BeautifulSoup(html, "html.parser")

    # soup = BeautifulSoup(open('example.html', encoding='utf-8'), 'html.parser')
    #  nodes = soup.find_all('tr', class_=[" ", "jtgs_table_td_bg"])
    nodes = soup.find_all('tr', attrs={'onmousemove': 'cursorOver(this)'})
    for node in nodes:
        date = node.select('td:nth-of-type(4)')[0].text
        if isToday(date):
            onclick = node.attrs['onclick']
            biddingId = re.findall(r"\d+", onclick)
            ids = ids + biddingId
            logging.info("today's bidding id:" + biddingId[0])
    return ids

def getTodayTitleInfo():
    data = urllib.urlencode(params)
    response =urllib2.urlopen(listUrl,data)
    html = response.read().decode('utf-8')
    soup = BeautifulSoup(html, "html.parser")
    nodes = soup.find_all('tr', attrs={'onmousemove': 'cursorOver(this)'})
    for node in nodes:
        datestr = node.select('td:nth-of-type(4)')[0].text
        date=datetime.datetime.strptime(datestr, '%Y-%m-%d')
        nowstr = time.strftime('%Y-%m-%d',time.localtime(time.time()))
        nowdate = datetime.datetime.strptime(nowstr, '%Y-%m-%d')
        if (date - nowdate).days == 0:
            global todayids
            onclick = node.attrs['onclick']
            biddingId = re.findall(r"\d+", onclick)
            todayids=todayids+biddingId

def getTodayLocalInfo():
    data = urllib.urlencode(params2)
    response =urllib2.urlopen(listUrl,data)
    html = response.read().decode('utf-8')
    soup = BeautifulSoup(html, "html.parser")
    nodes = soup.find_all('tr', attrs={'onmousemove': 'cursorOver(this)'})
    for node in nodes:
        datestr = node.select('td:nth-of-type(4)')[0].text
        date=datetime.datetime.strptime(datestr, '%Y-%m-%d')
        nowstr = time.strftime('%Y-%m-%d',time.localtime(time.time()))
        nowdate = datetime.datetime.strptime(nowstr, '%Y-%m-%d')
        if (date - nowdate).days == 0:
            global todayids
            onclick = node.attrs['onclick']
            biddingId = re.findall(r"\d+", onclick)
            todayids=todayids+biddingId

def getTodayInfoNum():
    getTodayTitleInfo()
    getTodayLocalInfo()
    return len(list(set(todayids)))

def getTodayIds():
    return list(set(getLocalMatch() + getTitlematch()))

def isToday(dateStr):
    dateStr = dateStr + ' 23:59:59'
    date = datetime.datetime.strptime(dateStr, '%Y-%m-%d %H:%M:%S')
    now = datetime.datetime.now()
    return (date - now).days >= -1

'''
根据招标ID返回一个字典，
title：招标名称
content：招标内容
attachmentExist：是否有附件
fileName：附件名称
url：附件地址
'''
def getDetail(biddingId):
    print(biddingId)
    req = urllib2.Request(contentUrl + biddingId)
    response = urllib2.urlopen(contentUrl + biddingId)
    html = response.read().decode('utf-8')
    soup = BeautifulSoup(html, "lxml")
    title = soup.find('h1').text

    [s.extract() for s in soup(['input', 'img'])]
    try:
        content = str(soup.select('#mobanDiv')[0])
    except IndexError, e:
        logging.error(e.message)
        return None
    # for node in soup.findAll('tr'):
    #     content += str(node)
    content = "<div align='center'><a href='https://b2b.10086.cn/b2b/main/viewNoticeContent.html?noticeBean.id=" + biddingId + "' target='_blank'><h3>点此跳转到原始页面</h3></a><br/>" + content + "</div>"
    attachment = findAttachmentId(soup)
    if None != attachment:
        return dict({'id': biddingId, 'title': title, 'content': content, 'attachmentExist': True}, **attachment)
    return {'id': biddingId, 'title': title, 'content': content, 'attachmentExist': False}

def findAttachmentId(html):
    fontNodes = html.select("font[color='blue']")
    if len(fontNodes) == 0:
        return None
    fontNode = fontNodes[0]
    fileName = fontNode.text
    idNode = fontNode.parent
    attachmentUrl = attachmentUrlPre + idNode.attrs['href']
    return {'fileName': fileName, 'url': attachmentUrl}


def putTextInStr(content, node):
    if hasChild(node):
        content = content + node.text
        for child in node.children:
            putTextInStr(content, child)
    else:
        return


def hasChild(node):
    if isinstance(node, str):
        return False

    count = 0;
    for child in node.children:
        if isinstance(child, Tag):
            count = count + 1
    return count != 0


def download(url):
    return urllib.urlretrieve(url)


if __name__ == "__main__":
    for i in getTodayIds():
        print(i)

